﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using AspNet.StarterKits.Classifieds.Web;

public partial class Controls_CategorybyTreeView : System.Web.UI.UserControl
{
    public event CategorySelectionChangedEventHandler CategorySelectionChanged;
    public int Category
    {
        get
        {
            if (ViewState["categoryid"] != null)
            {
                return (int)ViewState["categoryid"];
            }
            return 0;
        }
        set { ViewState["categoryid"] = value; }
    }
    private int currParent = 0;
    private int oldParent = -1;
    private bool hasSub = true;
    private ArrayList activeParent = new ArrayList();
    private ArrayList parentKeys = new ArrayList();
    protected void Page_Load(object sender, EventArgs e)
    {
        //try
        //{
        //    this.Category = int.Parse(Request.QueryString["c"]);
        //}
        //catch
        //{

        //    this.Category = 0;
        //}
        if (this.Category > 0)
        {
            CategoryCache.getActiveParentIds(this.Category, ref activeParent);
            activeParent.Add(this.Category);
        }
        this.parentKeys = CategoryCache.getParentKeys();
        
    }
    public void refresh()
    {
        if (this.Category > 0)
        {
            activeParent = null;
            CategoryCache.getActiveParentIds(this.Category, ref activeParent);
            activeParent.Add(this.Category);
        }
        this.parentKeys = CategoryCache.getParentKeys();
        this.ReTreeView.DataBind();
    }
    protected void ReTreeView_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        CachedCategory category = e.Item.DataItem as CachedCategory;
        string liClass = "";
        string lnkClientId = "";
        LinkButton lnkChange = e.Item.FindControl("lnkCatChange") as LinkButton;
        if (lnkChange != null)
        {
            lnkClientId = lnkChange.ClientID;
            lnkChange.Text = category.NameWithActiveCount;
            lnkChange.CommandArgument = category.Id.ToString();
        }
        if (category != null)
        {
            if (this.activeParent.IndexOf(category.Id) != -1)
            {
                liClass = "Class='active'";
            }
            else
            {
                liClass = "";
            }
            Literal htmlItem = e.Item.FindControl("htmlItem") as Literal;
            if (this.parentKeys.IndexOf(category.Id) != -1)
            {
                if (this.oldParent == category.ParentCategoryId)
                {
                    htmlItem.Text = string.Format("</ul></li><li  {1}><a datafld='{3}' href='search.aspx?c={2}'>{0}</a><ul>", category.Name, liClass, category.Id,lnkClientId);
                }
                else
                {
                    if (!this.hasSub)
                    {
                        htmlItem.Text = string.Format("</ul></li><li {1}><a  datafld='{3}' href='search.aspx?c={2}'>{0}</a><ul>", category.Name, liClass, category.Id, lnkClientId);
                    }
                    else
                        htmlItem.Text = string.Format("<li {1}><a  datafld='{3}' href='search.aspx?c={2}'>{0}</a><ul>", category.Name, liClass, category.Id,lnkClientId);
                }

                this.oldParent = category.ParentCategoryId;
                this.currParent = category.Id;
                this.hasSub = true;


            }
            else
            {
                if (this.currParent!=category.ParentCategoryId)
                {
                    //if (!hasSub)
                    //{
                    //    htmlItem.Text = string.Format("</ul></li><li {1}><a href='search.aspx?c={2}'>{0}</a></li>", category.Name, liClass, category.Id);
                    //}
                    //else
                    htmlItem.Text = string.Format("</ul></li><li {1}><a datafld='{3}' href='search.aspx?c={2}'>{0}</a></li>", category.Name, liClass, category.Id,lnkClientId);
                    this.currParent = category.ParentCategoryId;
                }
                else
                    htmlItem.Text = string.Format("<li {1}><a datafld='{3}' href='search.aspx?c={2}'>{0}</a></li>", category.Name, liClass, category.Id, lnkClientId);
                hasSub = false;
            }
            
        }
        //        if (!this.hasSub)
        //        {
        //            if (category.ParentCategoryId != this.currParent)
        //                htmlItem.Text = string.Format("</li></ul><li {1}><a href='search.aspx?c={2}'>{0}</a><ul>", category.Name, liClass, category.Id);
        //            else
        //            htmlItem.Text = string.Format("<li {1}><a href='search.aspx?c={2}'>{0}</a><ul>", category.Name, liClass, category.Id);
        //            this.hasSub = true;
        //        }
        //        else
        //        {
        //            if (category.ParentCategoryId == this.currParent)
        //            {
        //                htmlItem.Text = string.Format("<li {1}><a href='search.aspx?c={2}'>{0}</a><ul>", category.Name, liClass, category.Id);
        //            }
        //            else
        //            {
        //                htmlItem.Text = string.Format("</li></ul><li {1}><a href='search.aspx?c={2}'>{0}</a><ul>", category.Name, liClass, category.Id);
        //                this.hasSub = false;
        //            }
        //        }

        //        if (this.currParent != category.Id)
        //        {
        //            this.oldParent = this.currParent;
        //            this.currParent = category.Id;
        //        }
                
        //    }
        //    else
        //    {
        //        if (this.currParent == category.ParentCategoryId)
        //            htmlItem.Text = string.Format("<li {1}><a href='search.aspx?c={2}'>{0}</a></li>", category.Name, liClass, category.Id);
        //        else
        //        {
        //            htmlItem.Text = string.Format("</li></ul><li {1}><a href='search.aspx?c={2}'>{0}</a></li>", category.Name, liClass, category.Id);
                    
        //        }
        //        this.hasSub = false;
              
        //    }
        //    //if (this.currParent != category.ParentCategoryId)
        //    //{
        //    //    this.oldParent = this.currParent;
        //    //    this.currParent = category.ParentCategoryId;
        //    //}

        //}
    }
    protected void OnCategorySelectionChanged(CategorySelectionChangedEventArgs e)
    {
        if (CategorySelectionChanged!=null)
        {
            CategorySelectionChanged(this, e);
        }
            
    }
    protected void ReTreeView_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        int catId = 0;
        int.TryParse(e.CommandArgument.ToString(), out catId);
        switch (e.CommandName)
        {
            case "categoryChanged":
                CategorySelectionChangedEventArgs _event = new CategorySelectionChangedEventArgs(catId);
                OnCategorySelectionChanged(_event);
                break;
            default:
                break;
        }
    }
}
